package com.offcn.sellergoods.controller;

import com.offcn.common.entity.PageResult;
import com.offcn.common.entity.Result;
import com.offcn.common.entity.StatusCode;
import com.offcn.sellergoods.pojo.Brand;
import com.offcn.sellergoods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 品牌管理
 *
 * @author 30389
 * CrossOrigin 跨域注解
 */
@RestController
@RequestMapping("brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询所有
     *
     * @return 返回查询到的所有数据
     */
    @GetMapping
    public Result findAll() {
        try {
            List<Brand> list = brandService.findAll();
            return new Result(true, StatusCode.OK, "查询成功", list);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 主键查询
     *
     * @return 返回查询到的数据
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Long id) {
        try {
            Brand brand = brandService.findById(id);
            return new Result(true, StatusCode.OK, "查询成功", brand);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 添加品牌
     *
     * @param brand 品牌数据
     * @return 返回是否添加成功
     */
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        try {
            brandService.add(brand);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    /**
     * 修改品牌
     *
     * @param brand 名牌数据
     * @return 返回是否修改成功
     */
    @PutMapping
    public Result update(@RequestBody Brand brand) {
        try {
            brandService.update(brand);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    /**
     * 主键删除
     *
     * @param id id
     * @return 返回是否删除成功呢
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id) {
        try {
            brandService.delete(id);
            return new Result(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除失败");
        }

    }

    /**
     * 条件查询
     *
     * @param brand
     * @return 返回通过条件查询到的数据
     */
    @PostMapping("search")
    public Result searchList(@RequestBody Brand brand) {
        try {
            List<Brand> list = brandService.findList(brand);
            return new Result(true, StatusCode.OK, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 分页查询
     *
     * @param page 当前页数
     * @param size 每页显示记录数
     * @return
     */
    @GetMapping("search/{page}/{size}")
    public Result searchPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        try {
            PageResult<Brand> pageResult = brandService.page(page, size);
            return new Result(true, StatusCode.OK, "查询成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 分页 + 条件查询
     * @param brand
     * @param page  当前页数
     * @param size  每页显示记录数
     * @return  返回查询到并进行了分页的内容
     */
    @PostMapping("search/{page}/{size}")
    public Result searchListPage(@RequestBody Brand brand,@PathVariable("page") int page, @PathVariable("size") int size){
        try {
            PageResult<Brand> pageResult= brandService.findPage(brand,page,size);
            return new Result(true, StatusCode.OK, "查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 查询品牌下拉列表
     * @return
     */
    @GetMapping("options")
    public Result options(){
        try {
            List<Map> options = brandService.findOptions();
            return new Result(true, StatusCode.OK, "查询成功",options);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
}
